﻿@{
    ViewBag.Title = "FetchJokeByAjax";
}

<h2>获取笑话</h2>
<div class="am-g">
    <div class="am-u-sm-4">
        <a class="am-btn am-btn-warning" onclick="fetchImages(this)">获取图片</a>
    </div>
    <div class="am-u-sm-4">
        <a class="am-btn am-btn-success" onclick="fetchGif(this)">获取Gif</a>
    </div>
    <div class="am-u-sm-4">
        <a class="am-btn am-btn-danger" onclick="fetchTxt(this)">测试</a>
    </div>
</div>
@section Scripts{
    <script>
        $(function () {  
        })
        function fetchImages() {
            for (var i = 1; i <= 2; i++) {
                //此处需使用闭包
                (function (i) {
                    setTimeout(function () {
                        var fetchUrl = 'http://route.showapi.com/341-2?showapi_sign=3f0c698c4357477ba780198b22f4eeaf&showapi_appid=35037&maxResult=50&page=' + i;
                        getData(fetchUrl);
                    }, 1500 * i);
                })(i);

            }
        }

        function fetchGif() {

            for (var i = 1; i <= 2; i++) {
                //此处需使用闭包
                (function (i) {
                    setTimeout(function () {
                        var fetchUrl = 'http://route.showapi.com/341-3?showapi_sign=3f0c698c4357477ba780198b22f4eeaf&showapi_appid=35037&maxResult=50&page=' + i;
                        getData(fetchUrl);
                    }, 1500 * i);
                })(i);
            }
        }

        function fetchTxt() {
            for (var i = 1; i <= 2; i++) {
                //此处需使用闭包
                (function (i) {
                    setTimeout(function () {
                        var fetchUrl = 'http://route.showapi.com/341-1?showapi_sign=3f0c698c4357477ba780198b22f4eeaf&showapi_appid=35037&maxResult=50&page=' + i;
                        getText(fetchUrl);
                    }, 1500 * i);
                })(i);
            }

        }

        var fetchNum = 0;
        function getData(fetchUrl) {
            $.ajax({
                type: "GET",
                url: fetchUrl,
                dataType: "json",
                success: function (data) {
                    if (data.showapi_res_body && data.showapi_res_body.contentlist) {
                        $.each(data.showapi_res_body.contentlist, function (index, item) {
                            writeLog('pic', item.title, item.img, 'http://route.showapi.com');
                        })
                    }
                    else {
                        console.log(data);
                    }
                },
                beforeSend: function () {
                },
                complete: function () {
                    fetchNum++;
                    console.log(fetchNum, fetchUrl);
                }
            });
        }

        function getText(fetchUrl) {
            $.ajax({
                type: "GET",
                url: fetchUrl,
                dataType: "json",
                success: function (data) {
                    if (data.showapi_res_body && data.showapi_res_body.contentlist) {
                        $.each(data.showapi_res_body.contentlist, function (index, item) {
                            writeLog('txt', escape(item.title), escape(item.text), 'http://route.showapi.com');
                        })
                    }
                    else {
                        console.log(data);
                    }
                },
                beforeSend: function () {
                },
                complete: function () {
                    fetchNum++;
                    console.log(fetchNum, fetchUrl);
                }
            });
        }

        function writeLog(type, title, content, source) {
            $.ajax({
                type: "POST",
                url: '@Url.Action("SaveJoke", "AutoFetch")',
                dataType: "json",
                data: {
                    type: type,
                    title: title,
                    content: content,
                    source: source
                },
                success: function (data) {
                    //console.log('writeLog', data);
                }
            });
        }
    </script>
}
